package com.itheima.web.servlet;


import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.pageBean;
import com.itheima.service.BrandService;
import com.itheima.service.BrandServiceimp.brandServiceimp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService = new brandServiceimp();
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Brand> allBrands = brandService.findAll();
        //转化为json格式
        String json = JSON.toJSONString(allBrands);
        //设置编码
        response.setContentType("application/json;charset=utf-8");
        //输出
        response.getWriter().println(json);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        BufferedReader reader = request.getReader();
        String params=reader.readLine();
        Brand brand = JSON.parseObject(params, Brand.class);
        brandService.add(brand);
        // 判断是否添加成功，返回给前端
        response.getWriter().write("success");
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        BufferedReader reader = request.getReader();
        String params=reader.readLine();
        Brand brand = JSON.parseObject(params, Brand.class);
        brandService.update(brand);
        // 判断是否添加成功，返回给前端
        response.getWriter().write("success");
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String paramId = request.getParameter("id");

        // 确保参数存在并且是有效的数字
        if (paramId != null && !paramId.isEmpty()) {
            try {
                int brandId = Integer.parseInt(paramId);
                // 调用删除服务
                brandService.delete(brandId);
                response.getWriter().write("success");
            } catch (NumberFormatException e) {
                response.getWriter().write("error: Invalid id");
            }
        } else {
            response.getWriter().write("error: No id provided");
        }
    }
    public void deletebyids(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        int[] idss = JSON.parseObject(params, int[].class);
// 调用删除方法
        brandService.deletebyids(idss);
// 判断删除是否成功，返回给前端
        response.getWriter().write("success");
    }
    public void selectbypage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //接受当前页码和每页显示条数
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        int currentPage;
        int size;
        try {
            currentPage = Integer.parseInt(pageStr);
            size = Integer.parseInt(sizeStr);
        } catch (NumberFormatException e) {
            response.getWriter().write("error: Invalid page or size");
            return;
        }
// 调用删除方法
        pageBean<Brand> pagebean=brandService.findByPage(currentPage, size);
        String json = JSON.toJSONString(pagebean);
        response.setContentType("text/json;charset=utf-8");
// 判断删除是否成功，返回给前端
        response.getWriter().write(json);
    }
    public void selectbycondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        int currentPage;
        int size;
        try {
            currentPage = Integer.parseInt(pageStr);
            size = Integer.parseInt(sizeStr);
        } catch (NumberFormatException e) {
            response.getWriter().write("error: Invalid page or size");
            return;
        }
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        Brand brand = JSON.parseObject(params, Brand.class);
        System.out.println(brand.toString());
        pageBean<Brand> brands = brandService.findByCondition(currentPage,size,brand);
        String json = JSON.toJSONString(brands);
        System.out.println(json);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(json);
    }
}